<?php
class ModelCatalogdanhmuchotro extends Model {
	public function adddanhmuchotro($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "danhmuchotro SET sort_order = '" . (int)$data['sort_order'] . "'");
		
		$danhmuchotro_id = $this->db->getLastId();
		
		foreach ($data['danhmuchotro_info'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "danhmuchotro_info SET danhmuchotro_id = '" . (int)$danhmuchotro_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");	
		}
		
		$this->cache->delete('danhmuchotro');
	}
	
	public function editdanhmuchotro($danhmuchotro_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "danhmuchotro SET sort_order = '" . (int)$data['sort_order'] . "' WHERE danhmuchotro_id = '" . (int)$danhmuchotro_id . "'");
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "danhmuchotro_info WHERE danhmuchotro_id = '" . (int)$danhmuchotro_id . "'");
		
		foreach ($data['danhmuchotro_info'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "danhmuchotro_info SET danhmuchotro_id = '" . (int)$danhmuchotro_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
		}
		$this->cache->delete('danhmuchotro');
	}
	
	public function deletedanhmuchotro($danhmuchotro_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "danhmuchotro WHERE danhmuchotro_id = '" . (int)$danhmuchotro_id . "'");
			
		$this->cache->delete('danhmuchotro');
	}	
	
	public function getdanhmuchotro($danhmuchotro_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . " danhmuchotro WHERE danhmuchotro_id = '" . (int)$danhmuchotro_id . "'");
		
		return $query->row;
	}
	
	public function getdanhmuchotros($data = array()) {

			$sql = "SELECT * FROM " . DB_PREFIX . "danhmuchotro d LEFT JOIN " . DB_PREFIX . " danhmuchotro_info di ON (d.danhmuchotro_id = di.danhmuchotro_id) WHERE language_id = '" . (int)$this->config->get('config_language_id') . "'";
			
			$sort_data = array(
				'name',
				'sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}					

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}				
			
			$query = $this->db->query($sql);
		
			return $query->rows;

	}
	
	public function getdanhmuchotross($danhmuchotro_id) {
		$danhmuchotro_info_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "danhmuchotro_info WHERE danhmuchotro_id = '" . (int)$danhmuchotro_id . "'");
		
		foreach ($query->rows as $result) {
			$danhmuchotro_info_data[$result['language_id']] = array(
				'name'             => $result['name']
			);
		}
		
		return $danhmuchotro_info_data;
	}

	public function getTotaldanhmuchotros() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "danhmuchotro");
		
		return $query->row['total'];
	}	
}
?>